package com.example.domain;

import com.example.domain.dto.SysUserDto;
import com.example.domain.pojo.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ljs
 * @date 2025/7/21
 */
@Data
public class UserDetail implements UserDetails , Serializable {

    private static final long serialVersionUID = 1L;

    private Long userId;

    private SysUserDto sysUser;

    private String token;

    private Long currentTime;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<String> permissions = sysUser.getPermissions();
        if(permissions != null && !permissions.isEmpty()) {
            List<SimpleGrantedAuthority> collect =
                    permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
            return collect;
        }
       return Collections.emptyList();
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
